/**
 * Copyright &copy; 2012-2014 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.zouqone.smc.modules.sys.utils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.zouqone.smc.common.utils.CacheUtils;
import com.zouqone.smc.common.utils.SpringContextHolder;
import com.zouqone.smc.modules.sys.dao.DictDao;
import com.zouqone.smc.modules.sys.dao.DictitemDao;
import com.zouqone.smc.modules.sys.entity.Dict;
import com.zouqone.smc.modules.sys.entity.Dictitem;

/**
 * 字典工具类
 * @author ThinkGem
 * @version 2013-5-29
 */
public class DictUtils {
	
	private static DictDao dictDao = SpringContextHolder.getBean(DictDao.class);

	public static final String CACHE_DICT_MAP = "dictMap";
	public static final String CACHE_DICTITEM_LIST = "dictitemList";
	
	private static DictitemDao dictitemDao = SpringContextHolder.getBean(DictitemDao.class);
	
	public static Dict getDictByTypeValue(String value, String type){
		if (StringUtils.isNotBlank(type) && StringUtils.isNotBlank(value)){
			for (Dict dict : getDictList(type)){
				if (type.equals(dict.getType()) && value.equals(dict.getValue())){
					return dict;
				}
			}
		}
		return new Dict();
	}
	public static String getDictLabel(String value, String type, String defaultValue){
		if (StringUtils.isNotBlank(type) && StringUtils.isNotBlank(value)){
			for (Dict dict : getDictList(type)){
				if (type.equals(dict.getType()) && value.equals(dict.getValue())){
					return dict.getLabel();
				}
			}
		}
		return defaultValue;
	}
	
	public static String getDictLabels(String values, String type, String defaultValue){
		if (StringUtils.isNotBlank(type) && StringUtils.isNotBlank(values)){
			List<String> valueList = Lists.newArrayList();
			for (String value : StringUtils.split(values, ",")){
				valueList.add(getDictLabel(value, type, defaultValue));
			}
			return StringUtils.join(valueList, ",");
		}
		return defaultValue;
	}

	public static String getDictValue(String label, String type, String defaultLabel){
		if (StringUtils.isNotBlank(type) && StringUtils.isNotBlank(label)){
			for (Dict dict : getDictList(type)){
				if (type.equals(dict.getType()) && label.equals(dict.getLabel())){
					return dict.getValue();
				}
			}
		}
		return defaultLabel;
	}
	
	public static List<Dict> getDictList(String type){
		@SuppressWarnings("unchecked")
		Map<String, List<Dict>> dictMap = (Map<String, List<Dict>>)CacheUtils.get(CACHE_DICT_MAP);
		if (dictMap==null){
			dictMap = Maps.newHashMap();
			for (Dict dict : dictDao.findAllList(new Dict())){
				List<Dict> dictList = dictMap.get(dict.getType());
				if (dictList != null){
					dictList.add(dict);
				}else{
					dictMap.put(dict.getType(), Lists.newArrayList(dict));
				}
			}
			CacheUtils.put(CACHE_DICT_MAP, dictMap);
		}
		List<Dict> dictList = dictMap.get(type);
		if (dictList == null ){
			if(!StringUtils.isEmpty(type)){
				Dict param = new Dict();
				param.setType(type);
				dictList = dictDao.findList(param);
				dictMap.put(type, dictList);
			}else{
				dictList = Lists.newArrayList();
			}
		}
		return dictList;
	}
	
	/**
	 * 通过类型和值过滤字典列表
	 * @param type
	 * @param values
	 * @return
	 */
	public static List<Dict> getDictListByTypeValues(String type,String values){
		List<Dict> dictAllList = getDictList(type);
		List<Dict> dictList = null;
		if(!StringUtils.isEmpty(values)){
			String [] valueArray = values.split(",");
			Map<String,String> map = new HashMap<String, String>();
			dictList = Lists.newArrayList();
			for (String value : valueArray) {
				map.put(value, value);
			}
			for (Dict dict : dictAllList) {
				if(map.containsKey(dict.getValue())){
					dictList.add(dict);
				}
			}
		}else{
			dictList = dictAllList;
		}
		return dictList;
	}
	
	@SuppressWarnings("unchecked")
	public static List<Dictitem> getDictitemList(){
		List<Dictitem> dictitemList = (List<Dictitem>) UserUtils.getCache(CACHE_DICTITEM_LIST);
		if(dictitemList == null){
			dictitemList = dictitemDao.findAllList(new Dictitem());
		}
		
		return dictitemList;
	}
	
	public static Map<String,Dict> getDictMap(String type){
		Map<String,Dict> map = new HashMap<String, Dict>();
		List<Dict> dictAllList = getDictList(type);
		for (Dict dict : dictAllList) {
			map.put(dict.getValue(), dict);
		}
		return map;
	}
	
	public static Dict getDict(String id){
		return dictDao.get(id);
	}
}
